package org.jeecg.modules.yc.NB.protocol;

import org.apache.commons.lang.ArrayUtils;
import org.jeecg.modules.yc.NB.Model.YCAnalysisModel;
import org.jeecg.modules.yc.util.TransformationTool;

public class YCAnalysis {
    private static final byte checkLength_device = 11;
    private static final byte checkLength_terminal = 9;
    private static final int HFAndFF_device =  0xa66a;
    private static final int HFAndFF_terminal =  0xa88a;

    public static YCAnalysisModel analysis(byte[] data){
        if(ArrayUtils.isEmpty(data)){
            return null;
        }
        switch (data[1]){
            case (byte) HFAndFF_device:
                if(!checkDevice(data)){
                    return null;
                }
                return YCAnalysisModel.builder()
                        .head(arrayCopy(data,0,1)[0])
                        .deviceId(arrayCopy(data, 2, 2))
                        .fnc(data[4])
                        .frameContinuation(data[5])
                        .l(data[6])
                        .dataArea(arrayCopy(data, 7, data[6]))
                        .build();
            case (byte) HFAndFF_terminal:
                if(!checkTerminal(data)){
                    return null;
                }
                return YCAnalysisModel.builder()
                        .head(arrayCopy(data,0,1)[0])
                        .l(data[5])
                        .dataArea(arrayCopy(data, 6, data[5]))
                        .fnc(data[4])
                        .build();
            default:
               return null;
        }
    }
    public static boolean checkDevice(byte[] data){

        /*长度校验*/
        if(data.length < checkLength_device ){
            return false;
        }
        if(data[6] + checkLength_device != data.length){
            return false;
        }
        //校验帧头和帧尾
        if(((data[0] & 0xFF) << 8 | (data[1] & 0xFF)) != (HFAndFF_device & 0xFFFF) || ((data[data.length - 2] & 0xFF) << 8 | (data[data.length - 1] & 0xFF)) != (HFAndFF_terminal & 0xFFFF)){
            return false;
        }
        //校验数据长度
        return true;
    }
    public static boolean checkTerminal(byte[] data){//a88a006372007ac3a88a

        /*最短长度校验*/
        if(data.length < checkLength_terminal ){
            return false;
        }
        /*实际长度校验*/
        if(data[5] + checkLength_terminal != data.length){
            return false;
        }
        //校验帧头和帧尾
        return ((data[0] & 0xFF) << 8 | (data[1] & 0xFF)) == (HFAndFF_terminal & 0xFFFF) && ((data[data.length - 2] & 0xFF) << 8 | (data[data.length - 1] & 0xFF)) == (HFAndFF_terminal & 0xFFFF);
    }

    public static byte[] arrayCopy(byte[] source,int srcPos,int length){
        byte[] destByte = new byte[length];
        System.arraycopy(source,srcPos,destByte,0,length);
        return destByte;
    }

    public static void main(String[] args) {
        System.out.println(analysis(TransformationTool.hexStringBytes("A66A2044010152011D0058011E007E011D0083011D00B0011C002F011C007B011B008E011B00B8011A00B401190085011800B8011800BC011700C0011600B001160093011500A30114009F0113007A0113007C011200D20E42A8B5A88A",0,0)));
    }
}